<?php
// anti direct-access
if (!defined('MEKARSARI_WEBGIS'))
	exit('Direct access to this script is prohibited.');

function getDBFdata($filename){
	$result = array(
		'tertanam'			=> array(),
		'belum_tertanam'	=> array()
	);
	
	// open in read-only mode
	$file 		= dbase_open($filename, 0);
	if ($file) {
	  $record_numbers = dbase_numrecords($file); 
	  for ($i = 1; $i <= $record_numbers; $i++) {
		$row = dbase_get_record($file, $i);
		
		if( $row[3] == 1 ){ // TANAMAN = 1, tertanam
			$result['tertanam'][] = $row[0];
		}
		else{
			$result['belum_tertanam'][] = $row[0];
		}
	  }
	}
	
	return $result;
}

function updateDBF($db, $filename){
	// open in read-write mode
	$file = dbase_open($filename, 2);

	if ($file) {
		// gets the old row
		 $record_numbers = dbase_numrecords($file); 
		 
		// find planted palm
		$r = $db->get_results("SELECT DISTINCT no_pancang FROM `caramel_managerial`.`biodata_tanaman`", ARRAY_A);
		foreach( $r as $p ){
			$no_pancang[] = (int)$p['no_pancang'];
		}
		
		for($i=1; $i<=$record_numbers; $i++){
			$row = dbase_get_record($file, $i);unset($row['deleted']);
			$row[3] = 1;
			
			if( in_array($row[0], $no_pancang) ){
				dbase_replace_record($file, $row, $i);
			}	
		}
		dbase_close($file);
	}
}

//function debug($p){ print '<pre>';print_r($p);print'</pre>';}
function debug2($p){ foreach($p as $a) echo "(".(int)$a.")"; echo '<br>';}

function fakeExtent($oldExtent, $x, $y, $maxextent){
	
	$minx = $maxextent[0];
	$maxx = $maxextent[2];
	$miny = $maxextent[1];
	$maxy = $maxextent[3];
	
	$h = $oldExtent[3] - $oldExtent[1];
	$w = $oldExtent[2] - $oldExtent[0];	
	
	// 0,0 ada di kiri bawah
	$newExtent = array(
		$x-0.5*$w, // x left 0
		$y-0.5*$h, // y bottom 1
		
		$x+0.5*$w, // x right 2
		$y+0.5*$h // y top 3
	);
	
	// validation
	// x	
	if( $newExtent[0] < $minx ){ // left x
		$newExtent[0] = $minx;
		$newExtent[2] = $minx + $w;
	} elseif ( $newExtent[2] > $maxx ){ // right x
		$newExtent[0] = $maxx - $w;
		$newExtent[2] = $maxx;
	}

	// y
	if( $newExtent[1] < $miny ){ // low y
		$newExtent[1] = $miny; 
		$newExtent[3] = $miny + $h;
	} elseif ( $newExtent[3] > $maxy ){ // upper y
		$newExtent[1] = $maxy - $h;
		$newExtent[3] = $maxy;
	}
	return $newExtent;
}

//------------------------------------------
//------------------------------------------
// img2map - convert image coords to map coords
function img2map($width,$height,$point,$ext) {
   
   // valid point required
   if ($point->x && $point->y){

      // find degrees per pixel 
      $dpp_x = ($ext->maxx - $ext->minx)/$width; 
      $dpp_y = ($ext->maxy - $ext->miny)/$height;

      // calculate map coordinates
      $p[0] = $ext->minx + $dpp_x*$point->x;
      $p[1] = $ext->maxy - $dpp_y*$point->y;
   }

   return $p;
} // end img2map


//------------------------------------------
// map2img - convert map coords to image coords
function map2img($width,$height,$point,$ext) {

   // valid point required  
   if ($point->x && $point->y){ 

      // find pixels per degree        
      $ppd_x = $width/($ext->maxx - $ext->minx);
      $ppd_y = $height/($ext->maxy - $ext->miny);

      // calculate image coordinates        
      $p[0] =  $ppd_x * ($point->x - $ext->minx);
      $p[1] =  $height - $ppd_y * ($point->y - $ext->miny);
      settype($p[0],"integer");
      settype($p[1],"integer");
   }

   return $p;
} // end map2img

function ref2img($ref_x, $ref_y, $ref_w, $ref_h, $map_w, $map_h){	
	
	$x = $ref_x/$ref_w * $map_w;
	$y = $ref_y/$ref_h * $map_h;
	
	return array($x,$y);	
}

//------------------------------------------
// MarkSpot - return an HTML imagemap area tag 
function MarkSpot($seq,$width,$height,$point,$ext,$row) {

   // Given the map size in pixels and the geographic 
   // extent of the map returns an <area> tag that 
   // contains Javascript event handlers that popup
   // and hide tooltips on mouseovers. 

   // hotspot is point coordinates +/- $size pixels
   $size = 5;

   // get hotspot coords in pixels
   list($x, $y) = map2img($width,$height,$point,$ext);

   // calculate coordinates of imagemap area
   $xm = $x - $size; if ($xm < 0 ) {$xm = 0;}
   $ym = $y - $size; if ($ym < 0 ) {$ym = 0;}
   $xp = $x + $size; if ($xp > $width ) {$xp = $width;}
   $yp = $y + $size; if ($yp > $height ) {$yp = $height;}

   // create <area> tag
   $area = "<area name=\"$seq\" "; 
   $area = $area."onmouseover=\"return overlib('<div id=tooltip>$row[0]</div>');\" ";
   $area = $area."onmouseout=\"return nd();\" ";
   $area = $area."coords=\"$xm,$ym,$xp,$yp\"";
   $area = $area."onclick=\"processPos($xm, $ym, $xp, $yp, '$row[0]');\">\n";
   // return <area> tag
   return $area;
} // end MarkSpot


//------------------------------------------
// CreateTTimagemap - create atooltip imagemap
function CreateTTimagemap($qresult,$map) {
   // return an imagemap with an <area> tag 
   // for each row of the query results.

   $hotSpot = ms_newPointObj();

   $imagemap = "<map name=\"peta\">";

   // scan the query results
   for ( $i = 0; $i < count($qresult); $i++) {
      $row 	   = $qresult[$i];
      $hotSpot->setXY($row[1],$row[2]);
      $newarea = MarkSpot($i,$map->width,$map->height,$hotSpot,$map->extent,$row);
      $imagemap = $imagemap.$newarea;
   }

   // close the imagemap tag
   $imagemap = $imagemap."</map>";

   return $imagemap;
} // end CreateTTimageMap


//------------------------------------------
// AddPoints - add store locations to 'poi' map layer
function AddPointsToMap ( $map, $qresult ) {	
     // Use lat/long info from query results to add points 
     // to the points-of-interest layer of the map
     // shape index is set to the store-id 
     // (this requires a patched version of Mapscript)
     $this_layer = $map->getLayerByName('poi');

     $i = 0;
	 
     foreach($qresult as $row) {
        $poi[$i] 	= ms_newPointObj();
        $ln[$i] 	= ms_newLineObj();
        $shp[$i] 	= ms_newShapeObj(MS_SHAPE_POINT);
        
		$poi[$i]->setXY($row[1],$row[2]);
        
		$ln[$i]->add($poi[$i]);		
        $shp[$i]->add($ln[$i]);
        $shp[$i]->set(index, $row[0]);
        $this_layer->addFeature( $shp[$i] );
        $i++;
     }

     return;
} // end AddPoints

function fadeIn( $msg = 'hai', $timeout=1){
?>	
<style type="text/css">

#flashMsg {
	border: 1px solid #817558;		
	color: #58503D;	
	background-color:#BAB09A;
}

</style>
<script language="javascript" src="scripts/prototype.js"></script>
<script language="javascript" src="scripts/mootools.js"></script>
<script type="text/javascript">
window.addEvent('domready', function() {
	var inMsg = $('flashMsg');
	
	// We define the fade morph we're going to
	// use when firing an event
	var fade = new Fx.Morph(inMsg, {
		duration: <?=($timeout*1000);?>,
		link: 'cancel',
		transition: 'quad:in'
	});
	

	fade.start({
		backgroundColor: ['#ff0000', '#817558'],
		opacity: [0, 1],
	});
});
</script>
<div id="flashMsg"><?=$msg;?></div>
<?php
}

// get browser..
// by imamiscoo
function getBrowser(){
	$browser = array (
	  "MSIE",            // parent
	  "OPERA",
	  "MOZILLA",         // parent
	  "NETSCAPE",
	  "FIREFOX",
	  "SAFARI"
	);

	$info['agent'] = "OTHER";
	
	foreach ($browser as $parent) {		
		$s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent);
		$f = $s + strlen($parent);
		
		$version = substr($_SERVER['HTTP_USER_AGENT'], $f, 8);
		$version = preg_replace('/[^0-9,.]/','',$version);

		if ( $s !== FALSE ) {
			$info['agent'] = $parent;
			$info['version'] = $version;
		}
		
	}
	
	return $info;
}
?>